grass.script: Add locking to init #5591
Merged
+189
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The function gs.setup.init can now optionally lock the mapset and unlock when exiting the context or with call to finish (both free function or bound method). This keeps the current state of not locking as a default, i.e., the locking is opt-in, so multiple session are responsibility of the user just like running multiple processes within one mapset.
Importantly, this brings feature parity with the grass command to Python, and makes it possible for users to use the mapset locking from Python.
The PID used is the the current process ID which is already used for variable GIS_LOCK in init. The underlying locking (but not unlocking) functionality is the same one as used for the main grass command thanks to a previous refactoring of locking from lib/init/grass.py to grass.app.data.
The tests are included, but the thing about testing this is that when lock is found lib/init/lock.c (etc/lock) waits for a second before proceeding to check the file content. This makes each test with existing lock to take at least one second. There is currently six tests like that. The lock program wants to read the PID written to the lock file, so it waits for one second to ensure that another process wrote the content to the file. Without a lock preset, the operation is fast.